﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CMS.Web.Engine
{
    public enum WidgetSettingControlType
    {
        TextBox,
        HtmlEditor,
        DropDown,
        TextArea,
        CheckBox,
        Hidden,
        RadioButtonList,
        CheckBoxList
    }
    public enum WidgetSettingDataType
    {
        String,
        Integer
    }
    public class WidgetAttribute : Attribute
    {
        public string Guid;
        public string Name;
        public string Category;
        public string Description;     
        public string PartialViewName;
    }

    public class WidgetSettingAttribute : Attribute
    {
        public readonly string Title;
        public readonly string Description;
        public readonly string Category;
        public readonly WidgetSettingControlType ControlType;
        public readonly WidgetSettingDataType DataType;

        public WidgetSettingAttribute(string title, string description, string category, WidgetSettingControlType controlType, WidgetSettingDataType dataType)
        {
            this.Title = title;
            this.Description = description;
            this.Category = category;
            this.ControlType = controlType;
            this.DataType = dataType;
        }
        public WidgetSettingAttribute(string title, string description)
            : this(title, description, "Основные настройки", WidgetSettingControlType.TextBox, WidgetSettingDataType.String)
        { }

        public WidgetSettingAttribute(string title, string description, WidgetSettingControlType controlType)
            : this(title, description, "Основные настройки", controlType, WidgetSettingDataType.String)
        { }
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class ActionErrorAttribute : FilterAttribute, IExceptionFilter
    {
        public string ViewName { get; set; }
        public string MasterName { get; set; }
        public bool Enabled { get; set; }

        public virtual void OnException(ExceptionContext context)
        {
            if (context.IsNull())
                throw new ArgumentNullException("context");

            if (context.IsChildAction || context.ExceptionHandled || !this.Enabled || new HttpException(null, context.Exception).GetHttpCode() != 500)
                return;

            context.Result = new ViewResult
            {
                ViewName = this.ViewName,
                MasterName = this.MasterName,
                ViewData = new ViewDataDictionary<HandleErrorInfo>(new HandleErrorInfo(context.Exception,
                    (string)context.RouteData.Values["controller"], (string)context.RouteData.Values["action"])),
                TempData = context.Controller.TempData
            };

            context.ExceptionHandled = true;
        }
    }
}